<!--车辆管理页-->
<template>
<!--  车辆页面标题头部-->
  <div style="height: 6vh;background-color:#fff;padding:0px 20px 18px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="addVehicle()" type="primary" style="float:right;margin-top:13px;" >新建车辆</el-button>
  </div>
<!--  *********************************************************************************************-->
  <!-- 新增/编辑车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="saveVehicleForm.displacement">
              <el-option v-for="item in vehicleDisplacementArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="1.6" value="1"></el-option>-->
<!--              <el-option label="2.5" value="2"></el-option>-->
<!--              <el-option label="4" value="3"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.type">
              <el-option v-for="item in vehicleTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="轿车" value="10"></el-option>-->
<!--              <el-option label="客车" value="20"></el-option>-->
<!--              <el-option label="货车" value="30"></el-option>-->
<!--              <el-option label="挂车" value="40"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="saveVehicleForm.color">
              <el-option v-for="item in vehicleColorArr" :label="item.label" :value="item.value"></el-option>
<!--             <el-option v-for="item in vehicleColorArr" :label="item.label" :value="item.value"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveVehicleForm.buyTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveVehicleForm.regTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.batteryType">
              <el-option v-for="item in batteryTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="铅酸蓄电池" value="10"></el-option>-->
<!--              <el-option label="镍氢电池" value="20"></el-option>-->
<!--              <el-option label="钠硫电池" value="30"></el-option>-->
<!--              <el-option label="二次锂电池" value="40"></el-option>-->
<!--              <el-option label="空气电池" value="50"></el-option>-->
<!--              <el-option label="三元锂电池" value="60"></el-option>-->
<!--              <el-option label="碱性燃料电池" value="70"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle()">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input placeholder="请输入内容" style="width:220px;"
                  v-model="searchVehicleForm.brand" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;"
                  v-model="searchVehicleForm.license" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
<!--  *********************************************************************************************-->
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFomatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFomatter"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFomatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
<!--          这是在从查询到的数组里面获取值-->
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

// ***************************************************************************************
// 下面是车辆查询以及进去是就显示的数据
// ***************************************************************************************
//定义变量用来保存车辆的查询条件
const searchVehicleForm = ref({brand:'',license:''});
//定义数组用来保存车辆表格数据
const vehicleArr = ref([]);
//定义加载车辆的方法
const loadVehicle = () =>{
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL+'v1/vehicle/select?'+data).then((response)=>{
    if(response.data.code == 2000){
      vehicleArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
//定义重置搜索的方法
const resetSearch = ()=>{
  searchVehicleForm.value = {};
  loadVehicle();
}
onMounted(()=>{
  loadVehicle();
})
// 点击调用删除方法
const deleteVehicle = (id) =>{
  axios.post(BASE_URL+'v1/vehicle/delete/'+id)
      .then((response)=>{
       if (confirm('您确认要删除该车辆吗?')){
         if (response.data.code == 2000){
           ElMessage.success('删除车辆成功!');
           loadVehicle();
         }
         else {
           ElMessage.error(response.data.message);
         }
       }
      })
}
// **********************************************************************************************
// 下面是新增/编辑车辆的表单
// **********************************************************************************************
//控制新增车辆弹窗是否显示,默认不显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('');
// 这是点击新增车辆按钮后触发的方法
const addVehicle = () =>{
  dialogTitle.value = '新增车辆';
  dialogVisible.value = true;
}
// 编辑车辆按钮
const editVehicle = (id) =>{
  dialogTitle.value = '编辑车辆';
  dialogVisible.value = true;
  axios.get(BASE_URL+'v1/vehicle/select?id='+id)
      .then((response)=>{
        if(response.data.code == 2000){
          saveVehicleForm.value = response.data.data[0];
        }else{
          ElMessage.error(response.data.message);
        }
      })
}
// 这个表示在关弹窗之前执行的方法
const handleClose = () =>{
  if(confirm('您确认要关闭弹窗吗?')){
    saveVehicleForm.value = {};
    dialogVisible.value = false;
  }
}
// 这个表示新增/编辑车辆修改的信息双向绑定保存在这里
const saveVehicleForm = ref({
  id:'',
  brand:'',
  license:'',
  model:'',
  code:'',
  displacement:'',
  type:'',
  color:'',
  kilometers:'',
  buyTime:'',
  regTime:'',
  price:'',
  batteryType:'',
});
// 这是弹窗中的确认按钮的方法,点击后保存车辆信息
const saveVehicle = () =>{
  // 这两个if是非空检验
  if (!saveVehicleForm.value.license || saveVehicleForm.value.license.trim()==''){
    ElMessage.error('请填写车牌号');
    return;
  }
 if (!saveVehicleForm.value.brand || saveVehicleForm.value.brand.trim()==''){
   ElMessage.error('请填写车辆品牌');
   return;
 }
  let data = qs.stringify(saveVehicleForm.value);
  axios.post(BASE_URL+'v1/vehicle/save',data).then((response)=>{
    if(response.data.code == 2000){
      ElMessage.success(response.data.message);
      dialogVisible.value = false;
      loadVehicle();
      saveVehicleForm.value = {};
    }else if (response.data.code == 3001){
      ElMessage.error(response.data.data);
    }else{
      ElMessage.error(response.data.message);
    }
  })
}
// 定义购买时间和上牌时间先后顺序的方法
const validateDate = ()=>{
  const buyTime = saveVehicleForm.value.buyTime;
  const regTime = saveVehicleForm.value.regTime;
// 这里要注意与数据库里面的时间比大小相反，离得越远数字越大
  if (regTime && buyTime && new Date(buyTime) >= new Date(regTime)) {
    ElMessage.error('购买时间不能晚于上牌时间');
    saveVehicleForm.value.regTime = '';
  }}

// ********************************************************************************************************************
// 下面是车辆类型、车辆状态、车辆电池类型将数据库中的枚举值转换成中文,显示出来,放在表格中
// ********************************************************************************************************************
// 车辆颜色的数组
const vehicleColorArr = ref([]);
const vehicleTypeArr = ref([]);
const batteryTypeArr = ref([]);
const generalStatusArr = ref([]);
const vehicleDisplacementArr = ref([]);
// 执行字典的接口
onMounted(()=>{
  loadDictOption(vehicleColorArr,'vehicle_color');
  loadDictOption(vehicleTypeArr,'vehicle_type');
  loadDictOption(batteryTypeArr,'battery_type');
  loadDictOption(generalStatusArr,'general_status');
  loadDictOption(vehicleDisplacementArr,'vehicle_displacement');
})
// 要注意这一部分是枚举值转换成中文，在列表项里面显示
const vehicleTypeFomatter = (row,column,cellValue,index) =>{
  if (!vehicleTypeArr.value){
    return cellValue;
  }
  for (let item of vehicleTypeArr.value){
    if (cellValue == item.value){
      cellValue = item.label;
    }

  }
  return cellValue;
  // if (cellValue == 10){
  //   cellValue = '轿车';
  // }else if(cellValue==20) {
  //   cellValue = '客车';
  // }else if(cellValue==30){
  //     cellValue = '货车';
  //   }else if(cellValue==40){
  //     cellValue = '挂车';
  //   }
//  return cellValue;
}
const vehicleStatusFomatter = (row,column,cellValue,index) =>{
  if (!generalStatusArr.value){
    return cellValue;
  }
  for (let item of generalStatusArr.value){
    if (cellValue == item.value){
      cellValue = item.label;
    }
  }
  return cellValue;
  // if(cellValue == 1){
  //   cellValue = '空闲';
  // }else if(cellValue == 2){
  //   cellValue = '占用';
  // }
  // return cellValue;
}
const batteryTypeFomatter = (row,column,cellValue,index) =>{
  if (!batteryTypeArr.value){
    return cellValue;
  }
  for (let item of batteryTypeArr.value){
    if (cellValue == item.value){
      cellValue = item.label;
    }
  }
  // 若是将return cellValue放在for循环里面执行第一遍时就已经返回并且是不在进行循环了，放在外面执行循环，是让所有的cellValue
  // 都有了相应的值，再进行返回
  return cellValue;
  // if(cellValue == 10){
  //   cellValue = '铅酸蓄电池';
  // }else if(cellValue == 20){
  //   cellValue = '镍氢电池';
  // }else if(cellValue == 30){
  //   cellValue = '钠硫电池';
  // }else if(cellValue == 40){
  //   cellValue = '二次锂电池';
  // }else if(cellValue == 50){
  //   cellValue = '空气电池';
  // }else if(cellValue == 60){
  //   cellValue = '三元锂电池';
  // }else if(cellValue == 70){
  //   cellValue = '碱性燃料电池';
  // }
  // return cellValue;
}
</script>